﻿// WebZHX.Infrastructure/Services/FileLoggerService.cs
//using Microsoft.Extensions.Logging;
using Serilog;
using System;
using System.Diagnostics;

namespace Infrastructure.Services
{
    /// <summary>
    /// 文件日志服务
    /// </summary>
    public class FileLoggerService
    {
        private readonly ILogger _logger;

        public FileLoggerService()
        {
            _logger = Log.Logger;
        }

        /// <summary>
        /// 记录信息日志
        /// </summary>
        public void LogInformation(string message, string module = null, string operation = null)
        {
            if (!string.IsNullOrEmpty(module) && !string.IsNullOrEmpty(operation))
            {
                _logger.Information("[{Module}.{Operation}] {Message}", module, operation, message);
            }
            else if (!string.IsNullOrEmpty(module))
            {
                _logger.Information("[{Module}] {Message}", module, message);
            }
            else
            {
                _logger.Information("{Message}", message);
            }
        }

        /// <summary>
        /// 记录警告日志
        /// </summary>
        public void LogWarning(string message, string module = null, string operation = null)
        {
            if (!string.IsNullOrEmpty(module) && !string.IsNullOrEmpty(operation))
            {
                _logger.Warning("[{Module}.{Operation}] {Message}", module, operation, message);
            }
            else if (!string.IsNullOrEmpty(module))
            {
                _logger.Warning("[{Module}] {Message}", module, message);
            }
            else
            {
                _logger.Warning("{Message}", message);
            }
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        public void LogError(string message, Exception exception = null, string module = null, string operation = null)
        {
            if (!string.IsNullOrEmpty(module) && !string.IsNullOrEmpty(operation))
            {
                if (exception != null)
                {
                    _logger.Error(exception, "[{Module}.{Operation}] {Message}", module, operation, message);
                }
                else
                {
                    _logger.Error("[{Module}.{Operation}] {Message}", module, operation, message);
                }
            }
            else if (!string.IsNullOrEmpty(module))
            {
                if (exception != null)
                {
                    _logger.Error(exception, "[{Module}] {Message}", module, message);
                }
                else
                {
                    _logger.Error("[{Module}] {Message}", module, message);
                }
            }
            else
            {
                if (exception != null)
                {
                    _logger.Error(exception, "{Message}", message);
                }
                else
                {
                    _logger.Error("{Message}", message);
                }
            }
        }

        /// <summary>
        /// 记录异常日志
        /// </summary>
        public void LogException(Exception exception, string message = null, string module = null, string operation = null)
        {
            var logMessage = message ?? exception.Message;
            LogError(logMessage, exception, module, operation);
        }

        /// <summary>
        /// 记录API请求日志
        /// </summary>
        public void LogApiRequest(string requestPath, string requestMethod, long executionTime,
            string userId = null, string userName = null, string ipAddress = null, string userAgent = null)
        {
            _logger.Information(
                "API请求: {RequestMethod} {RequestPath} | 执行时间: {ExecutionTime}ms | 用户: {UserName}({UserId}) | IP: {IpAddress} | UserAgent: {UserAgent}",
                requestMethod, requestPath, executionTime, userName, userId, ipAddress, userAgent);
        }

        /// <summary>
        /// 性能监控日志
        /// </summary>
        public IDisposable LogPerformance(string operationName, string module = null)
        {
            return new PerformanceMonitor(this, operationName, module);
        }

        /// <summary>
        /// 性能监控内部类
        /// </summary>
        private class PerformanceMonitor : IDisposable
        {
            private readonly FileLoggerService _loggerService;
            private readonly string _operationName;
            private readonly string _module;
            private readonly Stopwatch _stopwatch;

            public PerformanceMonitor(FileLoggerService loggerService, string operationName, string module)
            {
                _loggerService = loggerService;
                _operationName = operationName;
                _module = module;
                _stopwatch = Stopwatch.StartNew();

                _loggerService.LogInformation($"开始执行操作: {_operationName}", _module, _operationName);
            }

            public void Dispose()
            {
                _stopwatch.Stop();
                _loggerService.LogInformation(
                    $"操作执行完成: {_operationName} | 耗时: {_stopwatch.ElapsedMilliseconds}ms",
                    _module,
                    _operationName);
            }
        }
    }
}